/**
* @Author： 兔兔 ^_^o~ 努力！
* @Date： 2020/11/22 18:52
 */

package form

import (
	"crypto/md5"
	"errors"
	"fmt"
	"ginFrameDemo/app/models"
	"ginFrameDemo/utils"
	"github.com/e421083458/gin_scaffold/public"
	"github.com/gin-gonic/gin"
)

//登陆校验
type LoginFormInput struct {
	UserName string `json:"username" comment:"账号" example:"账号" validate:"required"`
	Pwd string `json:"pwd" comment:"密码" example:"密码" validate:"required"`
}

//注册校验
type RegisterFormInput struct {
	UserName string `json:"username" comment:"账号" example:"账号" validate:"required"`
	Pwd string `json:"pwd" comment:"密码" example:"密码" validate:"required"`
}

//登陆
func (params *LoginFormInput) Login(this *gin.Context) error {
	//参数校验
	if err:=public.DefaultGetValidParams(this,params);err!=nil{
		utils.Error(this,utils.ValidRequestErrorCode,err)
	}
	UserInfo,err:=(&models.UserTest{}).FindUserName(this,params.UserName)
	if err!=nil{
		return errors.New("账号不存在！")
	}
	//获取加密后的密码
	pwdMd5 := fmt.Sprintf("%x", md5.Sum([]byte(params.Pwd)))
	if pwdMd5 != string(UserInfo.Pwd) {
		return errors.New("账号或者密码不正确！")
	}
	return nil
}